{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组的拼接",
   "id": "2caf67ea7fea91c5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:29:27.926940Z",
     "start_time": "2025-01-07T11:29:27.921804Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# 1. 根据轴连接的数组序列，concatenate没有改变数组的维度\n",
    "a = np.array([[1, 2], [3, 4]])\n",
    "b = np.array([[5, 6], [7, 8]])\n",
    "c = np.array([[9, 10], [11, 12]])\n",
    "# 要求a,b两个数组的维度相同\n",
    "arr = np.concatenate((a, b, c), axis=0)  # 按行连接\n",
    "print(arr)\n",
    "# [[ 1  2]\n",
    "#  [ 3  4]\n",
    "#  [ 5  6]\n",
    "#  [ 7  8]\n",
    "#  [ 9 10]\n",
    "#  [11 12]]\n",
    "arr = np.concatenate((a, b, c), axis=1)  # 按列连接\n",
    "print(arr)\n",
    "# [[ 1  2  5  6  9 10]\n",
    "#  [ 3  4  7  8 11 12]]"
   ],
   "id": "13df63a190449822",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2]\n",
      " [ 3  4]\n",
      " [ 5  6]\n",
      " [ 7  8]\n",
      " [ 9 10]\n",
      " [11 12]]\n",
      "[[ 1  2  5  6  9 10]\n",
      " [ 3  4  7  8 11 12]]\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:38:37.052740Z",
     "start_time": "2025-01-07T11:38:37.046313Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 堆叠 stack会改变数组的维度\n",
    "arr = np.stack((a, b, c), axis=1)  # 按列连接\n",
    "print(arr)\n",
    "# [[[ 1  2]\n",
    "#   [ 5  6]\n",
    "#   [ 9 10]]\n",
    "# \n",
    "#  [[ 3  4]\n",
    "#   [ 7  8]\n",
    "#   [11 12]]]\n",
    "print(arr.shape)  # (2, 3, 2\n",
    "\n",
    "arr = np.stack((a, b, c), axis=0)  # 按行连接\n",
    "print(arr)\n",
    "# [[[ 1  2]\n",
    "#   [ 3  4]]\n",
    "# \n",
    "#  [[ 5  6]\n",
    "#   [ 7  8]]\n",
    "# \n",
    "#  [[ 9 10]\n",
    "#   [11 12]]]\n",
    "print(arr.shape)  # (3, 2, 2)"
   ],
   "id": "3240bebc5282b0c0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 1  2]\n",
      "  [ 5  6]\n",
      "  [ 9 10]]\n",
      "\n",
      " [[ 3  4]\n",
      "  [ 7  8]\n",
      "  [11 12]]]\n",
      "(2, 3, 2)\n",
      "[[[ 1  2]\n",
      "  [ 3  4]]\n",
      "\n",
      " [[ 5  6]\n",
      "  [ 7  8]]\n",
      "\n",
      " [[ 9 10]\n",
      "  [11 12]]]\n",
      "(3, 2, 2)\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:45:31.978124Z",
     "start_time": "2025-01-07T11:45:31.973780Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 2. 根据轴进行堆叠，2维会变为3维\n",
    "arr = [np.arange(12).reshape(3, 4) for i in range(10)]\n",
    "# print(arr)\n",
    "print(len(arr))  # 10\n",
    "print(\"-\" * 50)\n",
    "\n",
    "res = np.stack(arr, axis=0)\n",
    "# print(res)\n",
    "print(res.shape)  # (10, 3, 4)"
   ],
   "id": "2a3501ac2322e0fe",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "--------------------------------------------------\n",
      "(10, 3, 4)\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:45:38.954243Z",
     "start_time": "2025-01-07T11:45:38.949241Z"
    }
   },
   "cell_type": "code",
   "source": [
    "res = np.stack(arr, axis=1)\n",
    "# print(res)\n",
    "print(res.shape)  # (3, 10, 4)"
   ],
   "id": "4cff73f08099449d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 10, 4)\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:46:42.911188Z",
     "start_time": "2025-01-07T11:46:42.907184Z"
    }
   },
   "cell_type": "code",
   "source": [
    "res = np.stack(arr, axis=2)  # axis=2和axis=-1效果一样\n",
    "# print(res)\n",
    "print(res.shape)  # (3, 4, 10)"
   ],
   "id": "cab18f5739504f75",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4, 10)\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T11:54:19.872092Z",
     "start_time": "2025-01-07T11:54:19.866236Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 3. 矩阵垂直拼接，vstack没有增加维数，类似于concatenate   ---vertical\n",
    "v1 = [[0, 1, 2, 3, 4, 5],\n",
    "      [6, 7, 8, 9, 10, 11]]\n",
    "v2 = [[12, 13, 14, 15, 16, 17],\n",
    "      [18, 19, 20, 21, 22, 23]]\n",
    "result = np.vstack((v1, v2))\n",
    "print(result)\n",
    "# [[ 0  1  2  3  4  5]\n",
    "#  [ 6  7  8  9 10 11]\n",
    "#  [12 13 14 15 16 17]\n",
    "#  [18 19 20 21 22 23]]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 4. 矩阵水平拼接，类似于concatenate  horizontal\n",
    "v1 = [[0, 1, 2, 3, 4, 5],\n",
    "      [6, 7, 8, 9, 10, 11]]\n",
    "v2 = [[12, 13, 14, 15, 16, 17],\n",
    "      [18, 19, 20, 21, 22, 23]]\n",
    "result = np.hstack((v1, v2))\n",
    "print(result)\n",
    "# [[ 0  1  2  3  4  5 12 13 14 15 16 17]\n",
    "#  [ 6  7  8  9 10 11 18 19 20 21 22 23]]"
   ],
   "id": "c5148251d2ecc56c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "--------------------------------------------------\n",
      "[[ 0  1  2  3  4  5 12 13 14 15 16 17]\n",
      " [ 6  7  8  9 10 11 18 19 20 21 22 23]]\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 分割",
   "id": "98ce6b1ebfe3b189"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:06:56.775031Z",
     "start_time": "2025-01-07T12:06:56.767459Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 1. 将一个数组分割为多个子数组\n",
    "'''\n",
    " numpy.split(ary, indices_or_sections,axis)\n",
    "参数说明：\n",
    "ary：被分割的数组\n",
    "indices_or_sections：是一个整数，就用该数平均切分，如果是一个数组，为沿轴切分的位置（左开右闭）\n",
    "axis：沿着哪个维度进行切向，默认为0，横向切分。为1时，纵向切分\n",
    "'''\n",
    "\n",
    "arr = np.arange(12).reshape(4, 3)\n",
    "print(arr)\n",
    "# [[ 0  1  2]\n",
    "#  [ 3  4  5]\n",
    "#  [ 6  7  8]\n",
    "#  [ 9 10 11]]\n",
    "\n",
    "b = np.split(arr, 3, axis=1)\n",
    "print(b)\n",
    "print(type(b))  # <class 'list'> \n",
    "print(type(b[0]))  # <class 'numpy.ndarray'> \n",
    "print(b[0].shape)  # (4, 1)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "b = np.split(arr, 4, axis=0)\n",
    "print(b)\n",
    "print(type(b))  # <class 'list'> \n",
    "print(type(b[0]))  # <class 'numpy.ndarray'> \n",
    "print(b[0].shape)  # (1, 3)\n"
   ],
   "id": "8f80c80749aa786",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2]\n",
      " [ 3  4  5]\n",
      " [ 6  7  8]\n",
      " [ 9 10 11]]\n",
      "[array([[0],\n",
      "       [3],\n",
      "       [6],\n",
      "       [9]]), array([[ 1],\n",
      "       [ 4],\n",
      "       [ 7],\n",
      "       [10]]), array([[ 2],\n",
      "       [ 5],\n",
      "       [ 8],\n",
      "       [11]])]\n",
      "<class 'list'>\n",
      "<class 'numpy.ndarray'>\n",
      "(4, 1)\n",
      "--------------------------------------------------\n",
      "[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]]), array([[ 9, 10, 11]])]\n",
      "<class 'list'>\n",
      "<class 'numpy.ndarray'>\n",
      "(1, 3)\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:10:18.000540Z",
     "start_time": "2025-01-07T12:10:17.995555Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#hsplit和vsplit的作用是将数组水平或垂直分割为多个子数组，返回的结果是列表。\n",
    "\n",
    "# 2.numpy.hsplit 函数用于水平分割数组，通过指定要返回的相同形状的数组数量来拆分原数组。\n",
    "c = np.hsplit(arr, 3)\n",
    "print(c[0].shape)  # (4, 1)\n",
    "print(type(c))  # <class 'list'> \n",
    "print(c)\n"
   ],
   "id": "81e10b342be399e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(4, 1)\n",
      "<class 'list'>\n",
      "[array([[0],\n",
      "       [3],\n",
      "       [6],\n",
      "       [9]]), array([[ 1],\n",
      "       [ 4],\n",
      "       [ 7],\n",
      "       [10]]), array([[ 2],\n",
      "       [ 5],\n",
      "       [ 8],\n",
      "       [11]])]\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:15:40.244133Z",
     "start_time": "2025-01-07T12:15:40.239370Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# random.random((2,6))生成一个2行6列的随机组\n",
    "d = np.random.random((2, 6))\n",
    "print(d)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "harr = np.floor(10 * np.random.random((2, 6)))  # floor函数取整\n",
    "print(harr)"
   ],
   "id": "7fd093ea6ecc11c8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.05083714 0.23328462 0.45230962 0.75432621 0.35899134 0.92550035]\n",
      " [0.07673685 0.53465442 0.04855847 0.23324496 0.44288143 0.78326641]]\n",
      "--------------------------------------------------\n",
      "[[7. 8. 2. 4. 8. 0.]\n",
      " [4. 9. 3. 5. 5. 8.]]\n"
     ]
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:27:01.231531Z",
     "start_time": "2025-01-07T12:27:01.226098Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 3.numpy.vsplit 沿着垂直轴分割\n",
    "a = np.arange(16).reshape(4, 4)\n",
    "\n",
    "print(a)\n",
    "\n",
    "b = np.vsplit(a, 2)\n",
    "print(type(b))  # <class 'list'> \n",
    "print(b[0].shape)  # (2, 4)\n",
    "print(b)"
   ],
   "id": "a6bdf6b9c1eea477",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n",
      "<class 'list'>\n",
      "(2, 4)\n",
      "[array([[0, 1, 2, 3],\n",
      "       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11],\n",
      "       [12, 13, 14, 15]])]\n"
     ]
    }
   ],
   "execution_count": 47
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# numpy inf和nan",
   "id": "3b84cd5609b01b76"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:33:45.105240Z",
     "start_time": "2025-01-07T12:33:45.100081Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "# inf表示无穷大，nan表示非数字\n",
    "# nan 是not a number的缩写，表示非数字，在numpy中表示一个非数字值，可以用来表示缺失数据\n",
    "\n",
    "'''\n",
    "C 语言中表示最大的正整数值是 0x7FFF FFFF，最小的负整数是 0x8000 0000。\n",
    "查阅资料后，发现 inf 表示无穷大，需要使用 float(‘inf’) 函数来转化，那么对应的就有\tfloat('-inf') 表示无穷小了。\n",
    "这样你就可以使用任意数来判断和它的关系了。\n",
    "那什么时候会出现inf呢？ 比如一个数字除以0，Python中会报错，但是numpy中会是一个inf或者-inf\n",
    "另外还有 nan，这种写法在 pandans 中常见，表示缺失的数据，所以一般用 nan 来表示。任何与其做运算结果都是 nan\n",
    "'''\n",
    "\n",
    "# 创建一个nan和inf #\n",
    "a = np.nan\n",
    "b = np.inf\n",
    "print(a, type(a))  # nan <class 'float'>\n",
    "print(b, type(b))  # inf <class 'float'>\n"
   ],
   "id": "8ab929d89d36a32a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nan <class 'float'>\n",
      "inf <class 'float'>\n"
     ]
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:36:09.690827Z",
     "start_time": "2025-01-07T12:36:09.685125Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# p.nan != np.nan\t结果 是TRUE\n",
    "# 所以我们可以使用这两个结合使用判断nan的个数\n",
    "print(np.nan == np.nan)  # False\n",
    "print(np.nan != np.nan)  # True\n",
    "\n",
    "print(np.nan + 1)  # nan和其他数运算的结果都是nan"
   ],
   "id": "234c2c774fd299a4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "nan\n"
     ]
    }
   ],
   "execution_count": 52
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:42:26.983481Z",
     "start_time": "2025-01-07T12:42:26.978137Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24, dtype=float).reshape(4, 6)\n",
    "\n",
    "# 将三行四列的数改成nan\n",
    "t[3, 4] = np.nan\n",
    "t[2, 4] = np.nan\n",
    "print(t)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(t != t)  # True表示该位置的元素为nan\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 可以使用np.count_nonzero() 来判断非零的个数\n",
    "# 因为false表示真值0，true表示真值1，所以这里统计的非零元素个数就是nan的个数\n",
    "print(np.count_nonzero(t != t))  # 统计nan的个数"
   ],
   "id": "c87e7ae8f0eb5f67",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. nan 17.]\n",
      " [18. 19. 20. 21. nan 23.]]\n",
      "--------------------------------------------------\n",
      "[[False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False False  True False]\n",
      " [False False False False  True False]]\n",
      "--------------------------------------------------\n",
      "2\n"
     ]
    }
   ],
   "execution_count": 58
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T12:43:25.609818Z",
     "start_time": "2025-01-07T12:43:25.606110Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将nan替换为0\n",
    "t[np.isnan(t)] = 0\n",
    "print(t)\n",
    "print(\"-\" * 50)"
   ],
   "id": "9bcea602c3eecc7a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15.  0. 17.]\n",
      " [18. 19. 20. 21.  0. 23.]]\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 60
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:08:28.902153Z",
     "start_time": "2025-01-07T13:08:28.897322Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 连续处理数组中的nan\n",
    "t = np.arange(24).reshape(4, 6).astype(float)\n",
    "print(t)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "t[1, 3:] = np.nan  #第1行第3列以及之后的列都设置为nan\n",
    "print(t)\n"
   ],
   "id": "38bfebd3190ab3b1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "--------------------------------------------------\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. nan nan nan]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n"
     ]
    }
   ],
   "execution_count": 69
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:08:31.180969Z",
     "start_time": "2025-01-07T13:08:31.173985Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将数组中的nan替换为所在列的均值\n",
    "\n",
    "# t.shape[1]表示列数\n",
    "for i in range(t.shape[1]):\n",
    "    # 获取当前列\n",
    "    tmp_col = t[:, i]\n",
    "\n",
    "    # 统计当前列中的nan的个数\n",
    "    nan_num = np.count_nonzero(tmp_col != tmp_col)\n",
    "    if nan_num > 0:\n",
    "        # 将这一列不为nan的数据拿出来,以计算均值\n",
    "        tmp_col_not_nan = tmp_col[tmp_col == tmp_col]\n",
    "        print(tmp_col_not_nan)\n",
    "        # 用均值替换nan\n",
    "        tmp_col[np.isnan(tmp_col)] = np.mean(tmp_col_not_nan)\n",
    "\n",
    "print(\"-\" * 50)\n",
    "print(t)"
   ],
   "id": "71e2cbd542c1d4ad",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3. 15. 21.]\n",
      "[ 4. 16. 22.]\n",
      "[ 5. 17. 23.]\n",
      "--------------------------------------------------\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8. 13. 14. 15.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n"
     ]
    }
   ],
   "execution_count": 70
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:15:51.755478Z",
     "start_time": "2025-01-07T13:15:51.750810Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(np.inf == np.inf)  # True\n",
    "print(np.inf > -np.inf)  # True\n",
    "\n",
    "# np.nan和任何数据运算的结果都是nan\n",
    "t1 = np.arange(24).reshape(4, 6).astype(float)\n",
    "t1[1, 3:] = np.nan\n",
    "t2 = np.arange(24).reshape(4, 6).astype(float)\n",
    "print(t1 + t2)\n",
    "# [[ 0.  2.  4.  6.  8. 10.]\n",
    "#  [12. 14. 16. nan nan nan]\n",
    "#  [24. 26. 28. 30. 32. 34.]\n",
    "#  [36. 38. 40. 42. 44. 46.]]"
   ],
   "id": "325a744cb4033f02",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "[[ 0.  2.  4.  6.  8. 10.]\n",
      " [12. 14. 16. nan nan nan]\n",
      " [24. 26. 28. 30. 32. 34.]\n",
      " [36. 38. 40. 42. 44. 46.]]\n"
     ]
    }
   ],
   "execution_count": 79
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:15:54.076532Z",
     "start_time": "2025-01-07T13:15:54.071960Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr = np.array([-1, 0])\n",
    "print(arr)  # [-1  0]\n",
    "print(arr[0] / arr[1])  # -inf\n",
    "\n",
    "print(np.nan + np.inf)  # nan"
   ],
   "id": "f3d2acf2808c7645",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1  0]\n",
      "-inf\n",
      "nan\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\32952\\AppData\\Local\\Temp\\ipykernel_11580\\714176484.py:3: RuntimeWarning: divide by zero encountered in scalar divide\n",
      "  print(arr[0]/arr[1]) # -inf\n"
     ]
    }
   ],
   "execution_count": 80
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 转置和轴滚动",
   "id": "ae9da70fff957a1d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:22:07.500175Z",
     "start_time": "2025-01-07T13:22:07.495178Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 对换数组的维度\n",
    "\n",
    "a = np.arange(12).reshape(3, 4)\n",
    "print(a)\n",
    "# [[ 0  1  2  3]\n",
    "#  [ 4  5  6  7]\n",
    "#  [ 8  9 10 11]]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 转置\n",
    "print(np.transpose(a))  # transpose函数可以对换数组的维度\n",
    "# [[ 0  4  8]\n",
    "#  [ 1  5  9]\n",
    "#  [ 2  6 10]\n",
    "#  [ 3  7 11]]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(a.T)  # 也可以使用属性T来对换数组的维度"
   ],
   "id": "d34234f66fcb6eab",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "--------------------------------------------------\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n",
      "--------------------------------------------------\n",
      "[[ 0  4  8]\n",
      " [ 1  5  9]\n",
      " [ 2  6 10]\n",
      " [ 3  7 11]]\n"
     ]
    }
   ],
   "execution_count": 82
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:30:05.231737Z",
     "start_time": "2025-01-07T13:30:05.227113Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# swapaxes函数可以对换数组的两个轴\n",
    "t1 = np.arange(24).reshape(6, 4)\n",
    "print(t1.shape) # (6, 4)\n",
    "print(t1)\n",
    "# [[ 0  1  2  3]\n",
    "#  [ 4  5  6  7]\n",
    "#  [ 8  9 10 11]\n",
    "#  [12 13 14 15]\n",
    "#  [16 17 18 19]\n",
    "#  [20 21 22 23]]\n",
    "print(\"-\" * 50)\n",
    "\n",
    "t2 = t1.swapaxes(0, 1)\n",
    "print(t2.shape) # (4, 6)\n",
    "print(t2)\n",
    "# [[ 0  4  8 12 16 20]\n",
    "#  [ 1  5  9 13 17 21]\n",
    "#  [ 2  6 10 14 18 22]\n",
    "#  [ 3  7 11 15 19 23]]\n"
   ],
   "id": "d5d8412657bcc184",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(6, 4)\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]]\n",
      "--------------------------------------------------\n",
      "(4, 6)\n",
      "[[ 0  4  8 12 16 20]\n",
      " [ 1  5  9 13 17 21]\n",
      " [ 2  6 10 14 18 22]\n",
      " [ 3  7 11 15 19 23]]\n"
     ]
    }
   ],
   "execution_count": 102
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:32:40.155050Z",
     "start_time": "2025-01-07T13:32:40.151401Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 如果是3维及以上，称为轴交换\n",
    "t3=np.arange(24).reshape(2, 3, 4)\n",
    "print(t3.shape) # (2, 3, 4)\n",
    "\n",
    "t4=t3.swapaxes(1,2)\n",
    "print(t4.shape) # (2, 4, 3)\n"
   ],
   "id": "7d9ac6d702cf9cfb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 3, 4)\n",
      "(2, 4, 3)\n"
     ]
    }
   ],
   "execution_count": 103
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:38:35.832101Z",
     "start_time": "2025-01-07T13:38:35.827128Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 轴滚动\n",
    "# 数组的轴滚动,swapaxes每次只能交换两个轴，没有rollaxis方便，默认情况下轴滚动最前面\n",
    "# rollaxis函数可以将数组的轴向右或向左移动\n",
    "# rollaxis(数组名，参数i,参数j) # 将i放在原数组第j个位置\n",
    "a=np.ones((3,4,5,6)) # ones函数生成一个n维向量\n",
    "print(a.shape) # (3, 4, 5, 6)\n",
    "\n",
    "b=np.rollaxis(a,3,1) # 将第3个轴向右移动到第1个位置的前面\n",
    "print(b.shape) # (3, 6, 4, 5)\n",
    "\n",
    "c=np.rollaxis(b,1,4) # 将第1个轴向右移动到第4个位置的前面\n",
    "print(c.shape) # (3, 4, 5, 6)"
   ],
   "id": "80639af9e9e24ef8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 4, 5, 6)\n",
      "(3, 6, 4, 5)\n",
      "(3, 4, 5, 6)\n"
     ]
    }
   ],
   "execution_count": 106
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:46:09.536592Z",
     "start_time": "2025-01-07T13:46:09.530955Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 数据拷贝，copy()和赋值的区别\n",
    "a = np.array([[1, 2, 3], [1, 2, 3]])\n",
    "b = a.copy()\n",
    "print(a)\n",
    "print(b)\n",
    "# 地址不一样\n",
    "print(id(a)) # 1543108290928\n",
    "print(id(b)) # 1543108292464"
   ],
   "id": "53a1e64e900c3433",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [1 2 3]]\n",
      "[[1 2 3]\n",
      " [1 2 3]]\n",
      "1543130231408\n",
      "1543130231504\n"
     ]
    }
   ],
   "execution_count": 108
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 写文件",
   "id": "5565d74eccae4e58"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-07T13:49:50.664098Z",
     "start_time": "2025-01-07T13:49:50.659569Z"
    }
   },
   "cell_type": "code",
   "source": [
    "names='zhangsan,lisi,wangwu,zhaoliu,sunqi'\n",
    "#写入文件\n",
    "with open('names.csv','w') as f:\n",
    "    f.write(names)\n",
    "    f.write('\\n')\n",
    "    f.write('12,23,34,45,56')"
   ],
   "id": "1a81e4a8ca8c613f",
   "outputs": [],
   "execution_count": 110
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
